<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta property="og:type" content="website">
<meta property="og:title" content="幽辰的博客">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/page/5/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="幽辰">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/page/5/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'en'
  };
</script>

  <title>幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/31/canvas%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/31/canvas%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5/" class="post-title-link" itemprop="url">canvas基础概念</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-31 14:35:56 / Modified: 20:17:14" itemprop="dateCreated datePublished" datetime="2020-08-31T14:35:56+08:00">2020-08-31</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/HTML/" itemprop="url" rel="index"><span itemprop="name">HTML</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="什么是Canvas"><a href="#什么是Canvas" class="headerlink" title="什么是Canvas"></a>什么是Canvas</h2><p>HTML5 <code>&lt;canvas&gt;</code> 元素用于图形的绘制，通过脚本 (通常是JavaScript)来完成.</p>
<p><code>&lt;canvas&gt;</code> 标签只是图形容器，您必须使用脚本来绘制图形。</p>
<p>你可以通过多种方法使用 canvas 绘制路径,盒、圆、字符以及添加图像。</p>
<h2 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">href</span>=<span class="string">&quot;./style.css&quot;</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span> /&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">canvas</span> <span class="attr">class</span>=<span class="string">&#x27;canvas&#x27;</span> <span class="attr">id</span>=<span class="string">&#x27;canvas&#x27;</span> &gt;</span></span><br><span class="line">        您的浏览器不支持canvas</span><br><span class="line">        <span class="comment">&lt;!-- 如果浏览器不支持canvas则显示标签中的文字 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&#x27;text/javascript&#x27;</span> <span class="attr">src</span>=<span class="string">&#x27;./script.js&#x27;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;canvas&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>)</span><br><span class="line">ctx.moveTo(<span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">ctx.lineTo(<span class="number">100</span>, <span class="number">100</span>)</span><br><span class="line">ctx.stroke() <span class="comment">// 绘画</span></span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.canvas</span> &#123;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="http://blogs-images.youchen-blog.cn/Snipaste_2020-08-31_14-54-15.png" alt="image-20200831145509191"></p>
<p>当我们这样设置之后，其实整个 <code>canvas</code> 标签会变为 100 * 100，而其本身画布大小是 300 * 150（默认画布大小），会将绘制好的样式缩小（或放大）为 100 * 100</p>
<h3 id="控制canvas画布大小"><a href="#控制canvas画布大小" class="headerlink" title="控制canvas画布大小"></a>控制canvas画布大小</h3><h4 id="1-可以对canvas标签直接添加宽高"><a href="#1-可以对canvas标签直接添加宽高" class="headerlink" title="1. 可以对canvas标签直接添加宽高"></a>1. 可以对canvas标签直接添加宽高</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">class</span>=<span class="string">&#x27;canvas&#x27;</span> <span class="attr">id</span>=<span class="string">&#x27;canvas&#x27;</span> <span class="attr">width</span>=<span class="string">100</span> <span class="attr">height</span>=<span class="string">100</span> &gt;</span></span><br><span class="line">    您的浏览器不支持canvas</span><br><span class="line"><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-可以在js中对canvas的宽高进行限制"><a href="#2-可以在js中对canvas的宽高进行限制" class="headerlink" title="2. 可以在js中对canvas的宽高进行限制"></a>2. 可以在js中对canvas的宽高进行限制</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;canvas&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>)</span><br><span class="line"><span class="comment">// 对canvas的宽高进行限制</span></span><br><span class="line">canvas.width = <span class="number">100</span>;</span><br><span class="line">canvas.height = <span class="number">100</span>;</span><br><span class="line"></span><br><span class="line">ctx.moveTo(<span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">ctx.lineTo(<span class="number">100</span>, <span class="number">100</span>)</span><br><span class="line">ctx.stroke() <span class="comment">// 绘画</span></span><br></pre></td></tr></table></figure>

<p><strong>注：</strong> 如果有超出，则超出部分不绘制</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/30/%E5%AA%92%E4%BD%93%E6%9F%A5%E8%AF%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/30/%E5%AA%92%E4%BD%93%E6%9F%A5%E8%AF%A2/" class="post-title-link" itemprop="url">媒体查询</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-08-30 21:42:54" itemprop="dateCreated datePublished" datetime="2020-08-30T21:42:54+08:00">2020-08-30</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-08-31 20:18:10" itemprop="dateModified" datetime="2020-08-31T20:18:10+08:00">2020-08-31</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/CSS/" itemprop="url" rel="index"><span itemprop="name">CSS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>根据屏幕的大小写出不同的样式</p>
<h3 id="媒体类型"><a href="#媒体类型" class="headerlink" title="媒体类型"></a>媒体类型</h3><ul>
<li><p>all（默认，针对所有屏幕）</p>
</li>
<li><p>screen</p>
</li>
<li><p>print（打印预览）</p>
</li>
<li><p>speech（屏幕阅读器）</p>
</li>
</ul>
<h3 id="媒体查询中的逻辑"><a href="#媒体查询中的逻辑" class="headerlink" title="媒体查询中的逻辑"></a>媒体查询中的逻辑</h3><ul>
<li>与（and）</li>
<li>或（,）</li>
<li>非（not）</li>
</ul>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">表示在屏幕 大于1024px 或 小于900px 的情况下 `<span class="selector-tag">body</span>` 标签变红</span><br><span class="line"><span class="keyword">@media</span> screen <span class="keyword">and</span> (min-width: <span class="number">1024px</span>),(max-width: <span class="number">900px</span>)&#123;</span><br><span class="line">    <span class="selector-tag">body</span> &#123;</span><br><span class="line">        <span class="attribute">background-color</span>: red;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">表示在屏幕 不大于1024px（小于等于1024） 并且 不小于900px（大于等于900） 的情况下 `<span class="selector-tag">body</span>` 标签变红</span><br><span class="line"><span class="keyword">@media</span> <span class="keyword">not</span> screen <span class="keyword">and</span> (min-width: <span class="number">1024px</span>) <span class="keyword">and</span> (max-width: <span class="number">900px</span>)&#123;</span><br><span class="line">    <span class="selector-tag">body</span> &#123;</span><br><span class="line">        <span class="attribute">background-color</span>: red;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">表示在屏幕 不大于1024px（小于等于1024） 或者 小于900px 的情况下 `<span class="selector-tag">body</span>` 标签变红</span><br><span class="line"><span class="keyword">@media</span> <span class="keyword">not</span> screen <span class="keyword">and</span> (min-width: <span class="number">1024px</span>),(max-width: <span class="number">900px</span>)&#123;</span><br><span class="line">    <span class="selector-tag">body</span> &#123;</span><br><span class="line">        <span class="attribute">background-color</span>: red;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>注：</strong> </p>
<ul>
<li>如果 <code>not</code> 后面为 <code>and</code>，表示否定 <code>and</code> 连接的所有</li>
<li>如果 <code>not</code> 后面为 <code>,</code>（或） ，表示否定 <code>,</code> 前面的所有</li>
</ul>
<h3 id="常用媒体特征表达式"><a href="#常用媒体特征表达式" class="headerlink" title="常用媒体特征表达式"></a>常用媒体特征表达式</h3><h3 id="屏幕宽度"><a href="#屏幕宽度" class="headerlink" title="屏幕宽度"></a>屏幕宽度</h3><ul>
<li>width</li>
<li>max-width</li>
<li>min-width</li>
</ul>
<h3 id="设备像素比"><a href="#设备像素比" class="headerlink" title="设备像素比"></a>设备像素比</h3><ul>
<li>-webkit-device-pixel-ratio</li>
<li>-webkit-max-device-pixel-ratio</li>
<li>-webkit-min-device-pixel-ratio</li>
</ul>
<h3 id="横屏或竖屏（orientation）"><a href="#横屏或竖屏（orientation）" class="headerlink" title="横屏或竖屏（orientation）"></a>横屏或竖屏（orientation）</h3><ul>
<li>landscape（横屏）</li>
<li>portrait（竖屏）</li>
</ul>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/28/webapp%E9%80%82%E9%85%8D-%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/28/webapp%E9%80%82%E9%85%8D-%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5/" class="post-title-link" itemprop="url">webapp适配--基础概念</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-08-28 16:35:32" itemprop="dateCreated datePublished" datetime="2020-08-28T16:35:32+08:00">2020-08-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-08-30 22:02:50" itemprop="dateModified" datetime="2020-08-30T22:02:50+08:00">2020-08-30</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/%E5%85%B6%E4%BB%96/" itemprop="url" rel="index"><span itemprop="name">其他</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="像素"><a href="#像素" class="headerlink" title="像素"></a>像素</h2><ul>
<li>分辨率</li>
<li>物理像素（设备像素）</li>
<li>CSS像素（逻辑像素，设备独立像素，如px）</li>
<li>设备像素比（dpr）</li>
<li>标清屏和高清屏</li>
<li>缩放</li>
<li>PPI/DPI</li>
</ul>
<h3 id="设备像素比"><a href="#设备像素比" class="headerlink" title="设备像素比"></a>设备像素比</h3><p>dpr = 设备像素 / CSS像素（缩放为1的情况下）</p>
<p>设备像素比只对横向或纵向进行比较</p>
<h3 id="缩放"><a href="#缩放" class="headerlink" title="缩放"></a>缩放</h3><p>缩放改变的是CSS像素的大小</p>
<h3 id="PPI"><a href="#PPI" class="headerlink" title="PPI"></a>PPI</h3><p>每英寸的物理像素点</p>
<p>PPI越大，每英寸的物理像素点就越多，就越精细，反之亦然</p>
<p><img src="C:\Users\myc\AppData\Roaming\Typora\typora-user-images\image-20200828165233196.png" alt="image-20200828165233196"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/27/HTML%E8%BF%9B%E9%98%B6%E7%9F%A5%E8%AF%86%E7%82%B9-HTML5%E5%B1%9E%E6%80%A7%E5%8F%98%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/27/HTML%E8%BF%9B%E9%98%B6%E7%9F%A5%E8%AF%86%E7%82%B9-HTML5%E5%B1%9E%E6%80%A7%E5%8F%98%E5%8C%96/" class="post-title-link" itemprop="url">HTML进阶知识点--HTML5属性变化</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-27 18:05:22 / Modified: 18:06:59" itemprop="dateCreated datePublished" datetime="2020-08-27T18:05:22+08:00">2020-08-27</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/HTML/" itemprop="url" rel="index"><span itemprop="name">HTML</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="input标签type属性新增属性值"><a href="#input标签type属性新增属性值" class="headerlink" title="input标签type属性新增属性值"></a>input标签type属性新增属性值</h2><table>
<thead>
<tr>
<th>属性值</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>tel</td>
<td>手机端不同</td>
</tr>
<tr>
<td>email</td>
<td>手机端键盘出现@</td>
</tr>
<tr>
<td>number</td>
<td>出现按钮，可以加减</td>
</tr>
<tr>
<td>url</td>
<td>手机端键盘出现<code>.com</code></td>
</tr>
<tr>
<td>Date</td>
<td>选取日、月、年</td>
</tr>
<tr>
<td>Month</td>
<td>选取月、年</td>
</tr>
<tr>
<td>Week</td>
<td>选取周和年 <font color="red">不实用</font></td>
</tr>
<tr>
<td>Time</td>
<td>选取时间（小时和分钟）</td>
</tr>
<tr>
<td>Datatime</td>
<td>选取时间、日、月、年（UTC时间） <font color="red">不实用</font></td>
</tr>
<tr>
<td>Datatime-local</td>
<td>选取时间、日、月、年（本地时间）</td>
</tr>
<tr>
<td>range</td>
<td>一个可拉伸的进度条（pc端）</td>
</tr>
<tr>
<td>search</td>
<td>搜索框（pc端）</td>
</tr>
<tr>
<td>color</td>
<td>颜色选择框（pc端）</td>
</tr>
</tbody></table>
<h2 id="表单新增属性"><a href="#表单新增属性" class="headerlink" title="表单新增属性"></a>表单新增属性</h2><h3 id="autocomplete属性"><a href="#autocomplete属性" class="headerlink" title="autocomplete属性"></a>autocomplete属性</h3><p>form 或 input 域应该拥有自动完成功能</p>
<blockquote>
<p><code>&lt;form autocomplete=&quot;on&quot;&gt;&lt;/form&gt;</code></p>
</blockquote>
<p>注意：autocomplete 适用于 <code>&lt;form&gt;</code> 标签，以及以下类型的 <code>&lt;input&gt;</code> 标签：text、search、url、telephone、email、password、datepickers、range、color</p>
<h3 id="autofocus属性"><a href="#autofocus属性" class="headerlink" title="autofocus属性"></a>autofocus属性</h3><ul>
<li><p>规定在页面加载时，域自动地获取焦点</p>
</li>
<li><p>autofocus 属性适用于所有 <code>&lt;input&gt;</code> 标签的类型</p>
</li>
</ul>
<p>语法：</p>
<blockquote>
<p><code>&lt;input autofocus=&quot;autofocus&gt;</code></p>
</blockquote>
<h3 id="multiple属性"><a href="#multiple属性" class="headerlink" title="multiple属性"></a>multiple属性</h3><p>规定输入域中可选择多个值</p>
<p>PS：multiple 属性适用于以下类型的 <code>&lt;input&gt;</code> 标签：email 和 file</p>
<blockquote>
<p><code>&lt;input type=&quot;file&quot; multiple=&quot;multiple&quot;&gt;</code></p>
</blockquote>
<h3 id="placeholder属性"><a href="#placeholder属性" class="headerlink" title="placeholder属性"></a>placeholder属性</h3><p>提供一种提示（hint），描述输入框所期待的值</p>
<p>PS：适用于 <code>&lt;input&gt;</code> 标签 type 属性值为：text、search、url、tel、email、password</p>
<h3 id="required属性"><a href="#required属性" class="headerlink" title="required属性"></a>required属性</h3><p>规定必须在提交之前填写输入域（不能为空）</p>
<blockquote>
<p><code>&lt;input type=&quot;text&quot; required=&quot;required&quot;&gt;</code></p>
</blockquote>
<p>减少前端开发人员写表单验证的压力</p>
<p>PS：适用于 <code>&lt;input&gt;</code> 标签 type 属性值为：text、search、url、tel、email、password、datepickers、number、checkbox、radio、file</p>
<h2 id="链接新增属性"><a href="#链接新增属性" class="headerlink" title="链接新增属性"></a>链接新增属性</h2><ol>
<li><p><code>&lt;link&gt;</code> 的sizes</p>
</li>
<li><p><code>&lt;base&gt;</code> 的target</p>
<ul>
<li>base 标签如果设置了 target 属性，就可以控制整个页面所有超链接的跳转方式</li>
</ul>
</li>
<li><p><code>&lt;a&gt;</code> 标签</p>
<ul>
<li><p>media = “”（表示对设备进行优化，handheld 对“手持”设备进行支持，tv 对“电视”设备进行支持</p>
</li>
<li><p>hreflang = “zh”（设置语言，这里设置语言是中文）</p>
</li>
<li><p>rel = “external”（设置超链接的引用，这里超链接为外部链接）</p>
</li>
</ul>
</li>
</ol>
<h2 id="其他属性"><a href="#其他属性" class="headerlink" title="其他属性"></a>其他属性</h2><h3 id="script"><a href="#script" class="headerlink" title="script"></a>script</h3><ul>
<li><p>defer：规定当页面已完成加载后，才会执行脚本</p>
</li>
<li><p>async：规定一旦脚本可用，将会异步执行</p>
</li>
</ul>
<h3 id="ol"><a href="#ol" class="headerlink" title="ol"></a>ol</h3><table>
<thead>
<tr>
<th>属性</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>start</td>
<td>起始值</td>
</tr>
<tr>
<td>reversed</td>
<td>倒序排列</td>
</tr>
</tbody></table>
<h3 id="html"><a href="#html" class="headerlink" title="html"></a>html</h3><p>manifest = “cache.manifest”（定义页面离线应用文件）（引号中内容cache可更改）</p>
<blockquote>
<p><code>&lt;html manifest=&quot;cache.manifest&quot;&gt;</code></p>
</blockquote>
<h3 id="style"><a href="#style" class="headerlink" title="style"></a>style</h3><p>scoped：内嵌CSS</p>
<p>可以写在html文件的任意位置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;style scoped&gt;</span><br><span class="line">    ...</span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-Animation%EF%BC%88%E5%8A%A8%E7%94%BB%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-Animation%EF%BC%88%E5%8A%A8%E7%94%BB%EF%BC%89/" class="post-title-link" itemprop="url">Flutter-Animation（动画）</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 17:05:29 / Modified: 17:05:46" itemprop="dateCreated datePublished" datetime="2020-08-26T17:05:29+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">class AnimationDemo extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;Animation&#39;),</span><br><span class="line">        elevation: 0,</span><br><span class="line">      ),</span><br><span class="line">      body: AnimationDemoHome(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">class _AnimationDemoHomeState extends State&lt;AnimationDemoHome&gt; with TickerProviderStateMixin &#123;</span><br><span class="line">  AnimationController animationDemoController;</span><br><span class="line">  Animation animation;</span><br><span class="line">  Animation animationColor;</span><br><span class="line">  CurvedAnimation curve;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line"></span><br><span class="line">    animationDemoController &#x3D; AnimationController(</span><br><span class="line">      &#x2F;&#x2F; value: 32,</span><br><span class="line">      &#x2F;&#x2F; lowerBound: 32,</span><br><span class="line">      &#x2F;&#x2F; upperBound: 100,</span><br><span class="line">      duration: Duration(seconds: 1),</span><br><span class="line">      vsync: this,</span><br><span class="line">    );</span><br><span class="line"></span><br><span class="line">    curve &#x3D; </span><br><span class="line">        CurvedAnimation(parent: animationDemoController, curve: Curves.bounceOut);</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; Tween中只能使用double</span><br><span class="line">    animation &#x3D; Tween(begin: 32.0, end: 100.0).animate(curve);</span><br><span class="line">    animationColor &#x3D; </span><br><span class="line">        ColorTween(begin: Colors.red, end: Colors.red[900])</span><br><span class="line">        .animate(curve);</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; animationDemoController.addListener(() &#123;</span><br><span class="line">    &#x2F;&#x2F;   &#x2F;&#x2F; print(&#39;$&#123;animationDemoController.value&#125;&#39;);</span><br><span class="line">    &#x2F;&#x2F;   setState(() &#123;&#125;);</span><br><span class="line">    &#x2F;&#x2F; &#125;);</span><br><span class="line"></span><br><span class="line">    animationDemoController.addStatusListener((AnimationStatus status) &#123;</span><br><span class="line">      print(status);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; animationDemoController.forward(); &#x2F;&#x2F; 开始播放动画</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  void dispose() &#123;</span><br><span class="line">    super.dispose();</span><br><span class="line"></span><br><span class="line">    animationDemoController.dispose();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Center(</span><br><span class="line">      child: AnimatedHeart(</span><br><span class="line">        animations: [</span><br><span class="line">          animation,</span><br><span class="line">          animationColor</span><br><span class="line">        ],</span><br><span class="line">        controller: animationDemoController,</span><br><span class="line">      )</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">class AnimatedHeart extends AnimatedWidget&#123;</span><br><span class="line">  final List animations;</span><br><span class="line">  final AnimationController controller;</span><br><span class="line"></span><br><span class="line">  AnimatedHeart(&#123;</span><br><span class="line">    this.animations,</span><br><span class="line">    this.controller,</span><br><span class="line">  &#125;): super(listenable: controller);</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return IconButton(</span><br><span class="line">        icon: Icon(Icons.favorite),</span><br><span class="line">        iconSize: animations[0].value,</span><br><span class="line">        color: animations[1].value,</span><br><span class="line">        onPressed: () &#123;</span><br><span class="line">          switch (controller.status) &#123;</span><br><span class="line">            case AnimationStatus.completed:</span><br><span class="line">              controller.reverse(); &#x2F;&#x2F; 反转播放动画</span><br><span class="line">              break;</span><br><span class="line">            default:</span><br><span class="line">              controller.forward();</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">      );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-RxDart/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-RxDart/" class="post-title-link" itemprop="url">Flutter-RxDart</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 17:04:45 / Modified: 17:05:08" itemprop="dateCreated datePublished" datetime="2020-08-26T17:04:45+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>响应式编程</p>
<h2 id="Observable"><a href="#Observable" class="headerlink" title="Observable"></a>Observable</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">@override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line"></span><br><span class="line">    Observable&lt;String&gt; _observable &#x3D;</span><br><span class="line">        &#x2F;&#x2F; Observable(Stream.fromIterable([&#39;hello&#39;, &#39;您好&#39;])); &#x2F;&#x2F; 第一种方法</span><br><span class="line">        &#x2F;&#x2F; Observable.fromFuture(Future.value(&#39;hello ~&#39;)); &#x2F;&#x2F; 第二种方法</span><br><span class="line">        &#x2F;&#x2F; Observable.fromIterable([&#39;hello&#39;, &#39;您好&#39;]); &#x2F;&#x2F; 第三种方法</span><br><span class="line">        &#x2F;&#x2F; Observable.just(&#39;hello ~&#39;); &#x2F;&#x2F; 第四种方法</span><br><span class="line">        Observable.periodic(Duration(seconds: 3), (x) &#x3D;&gt; x.toString()); &#x2F;&#x2F; 间隔时间，每隔一段时间调用computation</span><br><span class="line"></span><br><span class="line">    _observable.listen(print);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="PublishSubject"><a href="#PublishSubject" class="headerlink" title="PublishSubject"></a>PublishSubject</h2><p>类似于<code>Stream</code>中的<code>Controller</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">PublishSubject&lt;String&gt; _subject &#x3D; PublishSubject&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">_subject.listen((data) &#x3D;&gt; print(&#39;listeb 1: $data&#39;));</span><br><span class="line">_subject.add(&#39;hello ~&#39;);</span><br><span class="line">_subject.listen((data) &#x3D;&gt; print(&#39;listeb 2: $&#123;data.toUpperCase()&#125;&#39;));</span><br><span class="line">_subject.add(&#39;hola&#39;);</span><br><span class="line"></span><br><span class="line">_subject.close();</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E7%9B%91%E5%90%AC%E4%B8%8E%E8%AE%A2%E9%98%85%EF%BC%88Stream%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-%E7%9B%91%E5%90%AC%E4%B8%8E%E8%AE%A2%E9%98%85%EF%BC%88Stream%EF%BC%89/" class="post-title-link" itemprop="url">Flutter-监听与订阅（Stream）</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 17:02:04 / Modified: 17:04:29" itemprop="dateCreated datePublished" datetime="2020-08-26T17:02:04+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line">import &#39;dart:async&#39;;</span><br><span class="line"></span><br><span class="line">import &#39;package:flutter&#x2F;material.dart&#39;;</span><br><span class="line"></span><br><span class="line">class StreamDemo extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;StreamDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: StreamDemoHome(),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class StreamDemoHome extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _StreamDemoHomeState createState() &#x3D;&gt; _StreamDemoHomeState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _StreamDemoHomeState extends State&lt;StreamDemoHome&gt; &#123;</span><br><span class="line">  StreamSubscription _streamSubscription;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line"></span><br><span class="line">    print(&#39;Create a stream&#39;);</span><br><span class="line">    Stream&lt;String&gt; _streamDemo &#x3D; Stream.fromFuture(fetchData());</span><br><span class="line"></span><br><span class="line">    print(&#39;Start listening on a stream&#39;);</span><br><span class="line">    &#x2F;&#x2F; 添加订阅</span><br><span class="line">    _streamSubscription &#x3D; _streamDemo.listen(onData, onError: onError, onDone: onDone);</span><br><span class="line"></span><br><span class="line">    print(&#39;Initialize completed.&#39;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onDone() &#123;</span><br><span class="line">    print(&#39;Done!&#39;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onError(error) &#123;</span><br><span class="line">    print(&#39;Error: $error&#39;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void onData(String data) &#123;</span><br><span class="line">    print(&#39;$data&#39;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 暂停订阅</span><br><span class="line">  void _pauseStream()&#123;</span><br><span class="line">    print(&#39;Pause subscription&#39;);</span><br><span class="line">    _streamSubscription.pause();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 恢复订阅</span><br><span class="line">  void _resumeStream()&#123;</span><br><span class="line">    print(&#39;Resume subscription&#39;);</span><br><span class="line">    _streamSubscription.resume();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 取消订阅</span><br><span class="line">  void _cancelStream()&#123;</span><br><span class="line">    print(&#39;Cancel subscription&#39;);</span><br><span class="line">    _streamSubscription.cancel();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  Future&lt;String&gt; fetchData() async &#123;</span><br><span class="line">    await Future.delayed(Duration(seconds: 5));</span><br><span class="line">    throw &#39;Something happen&#39;;</span><br><span class="line">    &#x2F;&#x2F; return &#39;hello~&#39;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Container(</span><br><span class="line">      child: Center(</span><br><span class="line">        child: Row(</span><br><span class="line">          mainAxisAlignment: MainAxisAlignment.center,</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            FlatButton(</span><br><span class="line">              child: Text(&#39;Pause&#39;),</span><br><span class="line">              onPressed: _pauseStream,</span><br><span class="line">            ),</span><br><span class="line">            FlatButton(</span><br><span class="line">              child: Text(&#39;Resume&#39;),</span><br><span class="line">              onPressed: _resumeStream,</span><br><span class="line">            ),</span><br><span class="line">            FlatButton(</span><br><span class="line">              child: Text(&#39;Cancel&#39;),</span><br><span class="line">              onPressed: _cancelStream,</span><br><span class="line">            ),</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="控制器（controller）"><a href="#控制器（controller）" class="headerlink" title="控制器（controller）"></a>控制器（controller）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">StreamController&lt;String&gt; _streamDemo;</span><br><span class="line"></span><br><span class="line">@override</span><br><span class="line">void initState() &#123;</span><br><span class="line">  super.initState();</span><br><span class="line"></span><br><span class="line">  print(&#39;Create a stream&#39;);</span><br><span class="line">  _streamDemo &#x3D; StreamController&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">  print(&#39;Start listening on a stream&#39;);</span><br><span class="line">  &#x2F;&#x2F; 添加订阅</span><br><span class="line">  _streamSubscription &#x3D; _streamDemo.stream.listen(onData, onError: onError, onDone: onDone);</span><br><span class="line"></span><br><span class="line">  print(&#39;Initialize completed.&#39;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">void _addDataToStream() async &#123;</span><br><span class="line">    print(&#39;Add data to stream.&#39;);</span><br><span class="line"></span><br><span class="line">    String data &#x3D; await fetchData();</span><br><span class="line">    _streamDemo.add(data);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="sink"><a href="#sink" class="headerlink" title="sink"></a>sink</h2><p>一般来说，对于stream，我们使用stream输出，sink输入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">StreamSink _sinkDemo;</span><br><span class="line"></span><br><span class="line">@override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line"></span><br><span class="line">    print(&#39;Create a stream&#39;);</span><br><span class="line">    _streamDemo &#x3D; StreamController&lt;String&gt;();</span><br><span class="line">    _sinkDemo &#x3D; _streamDemo.sink;</span><br><span class="line"></span><br><span class="line">    print(&#39;Start listening on a stream&#39;);</span><br><span class="line">    &#x2F;&#x2F; 添加订阅</span><br><span class="line">    &#x2F;&#x2F; 当有数据时，通过stream流出</span><br><span class="line">    _streamSubscription &#x3D; _streamDemo.stream.listen(onData, onError: onError, onDone: onDone);</span><br><span class="line"></span><br><span class="line">    print(&#39;Initialize completed.&#39;);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">void _addDataToStream() async &#123;</span><br><span class="line">    print(&#39;Add data to stream.&#39;);</span><br><span class="line"></span><br><span class="line">    String data &#x3D; await fetchData();</span><br><span class="line">    _sinkDemo.add(data);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="多次订阅（broadcast）"><a href="#多次订阅（broadcast）" class="headerlink" title="多次订阅（broadcast）"></a>多次订阅（broadcast）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">@override</span><br><span class="line">  void initState() &#123;</span><br><span class="line">    super.initState();</span><br><span class="line"></span><br><span class="line">    print(&#39;Create a stream&#39;);</span><br><span class="line">    &#x2F;&#x2F; Stream&lt;String&gt; _streamDemo &#x3D; Stream.fromFuture(fetchData());</span><br><span class="line">    _streamDemo &#x3D; StreamController.broadcast();</span><br><span class="line">    _sinkDemo &#x3D; _streamDemo.sink;</span><br><span class="line"></span><br><span class="line">    print(&#39;Start listening on a stream&#39;);</span><br><span class="line">    &#x2F;&#x2F; 添加订阅</span><br><span class="line">    _streamSubscription &#x3D; _streamDemo.stream.listen(onData, onError: onError, onDone: onDone);</span><br><span class="line"></span><br><span class="line">    _streamSubscription &#x3D; _streamDemo.stream.listen(onDataTwo, onError: onError, onDone: onDone);</span><br><span class="line"></span><br><span class="line">    print(&#39;Initialize completed.&#39;);</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="StreamBuilder"><a href="#StreamBuilder" class="headerlink" title="StreamBuilder"></a>StreamBuilder</h2><p>根据Stream上的数据渲染组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">StreamBuilder(</span><br><span class="line">  stream: _streamDemo.stream,</span><br><span class="line">  initialData: &#39;...&#39;,</span><br><span class="line">  builder: (context, snapshot)&#123;</span><br><span class="line">    print(snapshot);</span><br><span class="line">    return Text(&#39;$&#123;snapshot.data&#125;&#39;);</span><br><span class="line">  &#125;,</span><br><span class="line">),</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86/" class="post-title-link" itemprop="url">Flutter-状态管理</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 17:01:23 / Modified: 17:01:32" itemprop="dateCreated datePublished" datetime="2020-08-26T17:01:23+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="InheritedWidget"><a href="#InheritedWidget" class="headerlink" title="InheritedWidget"></a>InheritedWidget</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 爷爷组件</span><br><span class="line">class StateManagementDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _StateManagementDemoState createState() &#x3D;&gt; _StateManagementDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _StateManagementDemoState extends State&lt;StateManagementDemo&gt; &#123;</span><br><span class="line">  int _count &#x3D; 0;</span><br><span class="line"></span><br><span class="line">  void _transform() &#123;</span><br><span class="line">    setState(() &#123;</span><br><span class="line">      _count +&#x3D; 1;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return CounterProvider(</span><br><span class="line">      count: _count,</span><br><span class="line">      transform: _transform,</span><br><span class="line">      child: Scaffold(</span><br><span class="line">          appBar: AppBar(</span><br><span class="line">            title: Text(&#39;StateManagementDemo&#39;),</span><br><span class="line">            elevation: 0,</span><br><span class="line">          ),</span><br><span class="line">          body: CounterWrapper()),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 父亲组件</span><br><span class="line">class CounterWrapper extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Counter();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 孙组件</span><br><span class="line">class Counter extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    final int count &#x3D; CounterProvider.of(context).count;</span><br><span class="line">    final VoidCallback transform &#x3D; CounterProvider.of(context).transform;</span><br><span class="line">    return Center(</span><br><span class="line">      child: ActionChip(label: Text(&#39;$count&#39;), onPressed: transform),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 状态管理组件</span><br><span class="line">class CounterProvider extends InheritedWidget &#123;</span><br><span class="line">  final int count;</span><br><span class="line">  final VoidCallback transform;</span><br><span class="line">  final Widget child;</span><br><span class="line"></span><br><span class="line">  CounterProvider(&#123;this.count, this.transform, this.child&#125;) : super(child: child);</span><br><span class="line"></span><br><span class="line">  static CounterProvider of(BuildContext context) &#x3D;&gt; context.inheritFromWidgetOfExactType(CounterProvider);</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  bool updateShouldNotify(InheritedWidget oldWidget) &#123;</span><br><span class="line">    return true;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="ScopedModel（第三方库）"><a href="#ScopedModel（第三方库）" class="headerlink" title="ScopedModel（第三方库）"></a>ScopedModel（第三方库）</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 爷爷组件</span><br><span class="line">class StateManagementDemo extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return ScopedModel(</span><br><span class="line">      model: CounterModel(),</span><br><span class="line">      child: Scaffold(</span><br><span class="line">        appBar: AppBar(</span><br><span class="line">          title: Text(&#39;StateManagementDemo&#39;),</span><br><span class="line">          elevation: 0,</span><br><span class="line">        ),</span><br><span class="line">        body: CounterWrapper(),</span><br><span class="line">        floatingActionButton: ScopedModelDescendant&lt;CounterModel&gt;(</span><br><span class="line">          rebuildOnChange: false,</span><br><span class="line">          builder: (context, _, model) &#x3D;&gt; FloatingActionButton(</span><br><span class="line">            child: Icon(Icons.add), </span><br><span class="line">            onPressed: model.transform</span><br><span class="line">          ),</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 父亲组件</span><br><span class="line">class CounterWrapper extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Counter();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 孙组件</span><br><span class="line">class Counter extends StatelessWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return ScopedModelDescendant&lt;CounterModel&gt;(</span><br><span class="line">      builder: (context, _, model)&#x3D;&gt;ActionChip(</span><br><span class="line">        label: Text(&#39;$&#123;model.count&#125;&#39;),</span><br><span class="line">        onPressed: model.transform,</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 状态管理组件</span><br><span class="line">class CounterModel extends Model &#123;</span><br><span class="line">  int _count &#x3D; 0;</span><br><span class="line">  int get count &#x3D;&gt; _count;</span><br><span class="line"></span><br><span class="line">  void transform() &#123;</span><br><span class="line">    _count +&#x3D; 1;</span><br><span class="line">    notifyListeners();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-%E6%95%B0%E6%8D%AE%E8%A1%A8%E6%A0%BC/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-%E6%95%B0%E6%8D%AE%E8%A1%A8%E6%A0%BC/" class="post-title-link" itemprop="url">Flutter-数据表格</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 16:55:59 / Modified: 16:56:17" itemprop="dateCreated datePublished" datetime="2020-08-26T16:55:59+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">class DataTableDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _DataTableDemoState createState() &#x3D;&gt; _DataTableDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _DataTableDemoState extends State&lt;DataTableDemo&gt; &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;DataTableDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: Container(</span><br><span class="line">        padding: EdgeInsets.all(16),</span><br><span class="line">        child: ListView(</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            DataTable(columns: [</span><br><span class="line">              DataColumn(label: Text(&#39;Title&#39;)),</span><br><span class="line">              DataColumn(label: Text(&#39;Author&#39;)),</span><br><span class="line">            ], rows: [</span><br><span class="line">              DataRow(</span><br><span class="line">                cells: [</span><br><span class="line">                  DataCell(Text(&#39;a&#39;)), </span><br><span class="line">                  DataCell(Text(&#39;b&#39;))</span><br><span class="line">                ]</span><br><span class="line">              )</span><br><span class="line">            ])</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-5cf7d3e8a92142b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="列表生成"><a href="#列表生成" class="headerlink" title="列表生成"></a>列表生成</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">child: ListView(</span><br><span class="line">  children: &lt;Widget&gt;[</span><br><span class="line">    DataTable(</span><br><span class="line">      columns: [</span><br><span class="line">        DataColumn(</span><br><span class="line">          label: Text(&#39;Title&#39;),</span><br><span class="line">        ),</span><br><span class="line">        DataColumn(</span><br><span class="line">          label: Text(&#39;Author&#39;)</span><br><span class="line">        ),</span><br><span class="line">        DataColumn(</span><br><span class="line">          label: Text(&#39;Author&#39;)</span><br><span class="line">        ),</span><br><span class="line">      ],</span><br><span class="line">      rows: posts.map(</span><br><span class="line">        (post)&#123;</span><br><span class="line">          return DataRow(</span><br><span class="line">            cells: [</span><br><span class="line">              DataCell(Text(post.title)),</span><br><span class="line">              DataCell(Text(post.author)),</span><br><span class="line">              DataCell(Image.network(post.imageUrl)),</span><br><span class="line">            ]</span><br><span class="line">          );</span><br><span class="line">        &#125;</span><br><span class="line">      ).toList(),</span><br><span class="line">    )</span><br><span class="line">  ],</span><br><span class="line">),</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-0a8268d78f4950dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">DataColumn(</span><br><span class="line">  label: Text(&#39;Title&#39;),</span><br><span class="line">  onSort: (int index, bool ascending) &#123;</span><br><span class="line">    setState(() &#123;</span><br><span class="line">      _sortColumnIndex &#x3D; index;</span><br><span class="line">      _sortAscending &#x3D; ascending;</span><br><span class="line"></span><br><span class="line">      posts.sort((a, b) &#123;</span><br><span class="line">        if (!ascending) &#123;</span><br><span class="line">          final c &#x3D; a;</span><br><span class="line">          a &#x3D; b;</span><br><span class="line">          b &#x3D; c;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return a.title.length.compareTo(b.title.length);</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line"> &#125;),</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-3c3e181ac77506ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="总的代码"><a href="#总的代码" class="headerlink" title="总的代码"></a>总的代码</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">class DataTableDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _DataTableDemoState createState() &#x3D;&gt; _DataTableDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _DataTableDemoState extends State&lt;DataTableDemo&gt; &#123;</span><br><span class="line">  int _sortColumnIndex;</span><br><span class="line">  bool _sortAscending &#x3D; false;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;DataTableDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: Container(</span><br><span class="line">        padding: EdgeInsets.all(16),</span><br><span class="line">        child: ListView(</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            DataTable(</span><br><span class="line">              sortColumnIndex: _sortColumnIndex, &#x2F;&#x2F; 选择相应的列进行排序</span><br><span class="line">              sortAscending: _sortAscending, &#x2F;&#x2F; true为升序，false为降序</span><br><span class="line">              onSelectAll: (bool value)&#123;</span><br><span class="line">                </span><br><span class="line">              &#125;,</span><br><span class="line">              columns: [</span><br><span class="line">                DataColumn(</span><br><span class="line">                    label: Text(&#39;Title&#39;),</span><br><span class="line">                    onSort: (int index, bool ascending) &#123;</span><br><span class="line">                      setState(() &#123;</span><br><span class="line">                        _sortColumnIndex &#x3D; index;</span><br><span class="line">                        _sortAscending &#x3D; ascending;</span><br><span class="line"></span><br><span class="line">                        posts.sort((a, b) &#123;</span><br><span class="line">                          if (!ascending) &#123;</span><br><span class="line">                            final c &#x3D; a;</span><br><span class="line">                            a &#x3D; b;</span><br><span class="line">                            b &#x3D; c;</span><br><span class="line">                          &#125;</span><br><span class="line"></span><br><span class="line">                          &#x2F;&#x2F; 1.compareTo(2); -1</span><br><span class="line">                          &#x2F;&#x2F; 1.compareTo(0); 1</span><br><span class="line">                          &#x2F;&#x2F; 1.compareTo(1); 0</span><br><span class="line">                          return a.title.length.compareTo(b.title.length);</span><br><span class="line">                        &#125;);</span><br><span class="line">                      &#125;);</span><br><span class="line">                    &#125;),</span><br><span class="line">                DataColumn(label: Text(&#39;Author&#39;)),</span><br><span class="line">                DataColumn(label: Text(&#39;Author&#39;)),</span><br><span class="line">              ],</span><br><span class="line">              rows: posts.map((post) &#123;</span><br><span class="line">                return DataRow(</span><br><span class="line">                    selected: post.selected,</span><br><span class="line">                    onSelectChanged: (bool value) &#123;</span><br><span class="line">                      &#x2F;&#x2F; value 表示当前行选中的状态，被选了则value&#x3D;true，否则就是false</span><br><span class="line">                      setState(() &#123;</span><br><span class="line">                        if (post.selected !&#x3D; value) &#123;</span><br><span class="line">                          post.selected &#x3D; value;</span><br><span class="line">                        &#125;</span><br><span class="line">                      &#125;);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    cells: [</span><br><span class="line">                      DataCell(Text(post.title)),</span><br><span class="line">                      DataCell(Text(post.author)),</span><br><span class="line">                      DataCell(Image.network(post.imageUrl)),</span><br><span class="line">                    ]);</span><br><span class="line">              &#125;).toList(),</span><br><span class="line">            )</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-6b6b959ac53605a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="分页表格数据"><a href="#分页表格数据" class="headerlink" title="分页表格数据"></a>分页表格数据</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line">class PostDataSource extends DataTableSource&#123;</span><br><span class="line">  final List&lt;Post&gt; _posts &#x3D; posts;</span><br><span class="line">  int _selectedCount &#x3D; 0;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  int get rowCount &#x3D;&gt; _posts.length; &#x2F;&#x2F; 行的数量</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  bool get isRowCountApproximate &#x3D;&gt; false; &#x2F;&#x2F; 如果不确定行的数量，可以让其为true</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  int get selectedRowCount &#x3D;&gt; _selectedCount;</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  DataRow getRow(int index) &#123; &#x2F;&#x2F; 每一行具体的内容</span><br><span class="line">    final Post post &#x3D; _posts[index];</span><br><span class="line"></span><br><span class="line">    return DataRow.byIndex(</span><br><span class="line">      index: index,</span><br><span class="line">      cells: &lt;DataCell&gt;[</span><br><span class="line">        DataCell(Text(post.title)),</span><br><span class="line">        DataCell(Text(post.author)),</span><br><span class="line">        DataCell(Image.network(post.imageUrl)),</span><br><span class="line">      ]</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  void _sort(getField(post), bool ascending)&#123; &#x2F;&#x2F; 第一个参数是一个函数，这个函数中的参数是post</span><br><span class="line">    _posts.sort((a, b)&#123;</span><br><span class="line">      if (!ascending) &#123;</span><br><span class="line">        final c &#x3D; a;</span><br><span class="line">        a &#x3D; b;</span><br><span class="line">        b &#x3D; c;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      final aValue &#x3D; getField(a);</span><br><span class="line">      final bValue &#x3D; getField(b);</span><br><span class="line"></span><br><span class="line">      return Comparable.compare(aValue, bValue); &#x2F;&#x2F; 等同于aValue.compareTo(bValue)</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    notifyListeners(); &#x2F;&#x2F; 在状态发生变化时（increment）通知所有用到了该model的子项更新状态</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class PaginatedDataTableDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _PaginatedDataTableDemoState createState() &#x3D;&gt; _PaginatedDataTableDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _PaginatedDataTableDemoState extends State&lt;PaginatedDataTableDemo&gt; &#123;</span><br><span class="line">  int _sortColumnIndex;</span><br><span class="line">  bool _sortAscending &#x3D; false;</span><br><span class="line"></span><br><span class="line">  final PostDataSource _postDataSource &#x3D; PostDataSource();</span><br><span class="line"></span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;PaginatedDataTableDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: Container(</span><br><span class="line">        padding: EdgeInsets.all(16),</span><br><span class="line">        child: ListView(</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            PaginatedDataTable(</span><br><span class="line">              header: Text(&#39;Posts&#39;),</span><br><span class="line">              source: _postDataSource,</span><br><span class="line">              rowsPerPage: 5, &#x2F;&#x2F; 每一页显示的行数</span><br><span class="line">              sortColumnIndex: _sortColumnIndex, &#x2F;&#x2F; 选择相应的列进行排序</span><br><span class="line">              sortAscending: _sortAscending, &#x2F;&#x2F; true为升序，false为降序</span><br><span class="line">              onSelectAll: (bool value)&#123;</span><br><span class="line"></span><br><span class="line">              &#125;,</span><br><span class="line">              columns: [</span><br><span class="line">                DataColumn(</span><br><span class="line">                    label: Text(&#39;Title&#39;),</span><br><span class="line">                    onSort: (int columnIndex, bool ascending) &#123;</span><br><span class="line">                      _postDataSource._sort((post) &#x3D;&gt; post.title.length, ascending);</span><br><span class="line"></span><br><span class="line">                      setState(() &#123;</span><br><span class="line">                        _sortColumnIndex &#x3D; columnIndex;</span><br><span class="line">                        _sortAscending &#x3D; ascending;</span><br><span class="line">                      &#125;);</span><br><span class="line">                    &#125;),</span><br><span class="line">                DataColumn(label: Text(&#39;Author&#39;)),</span><br><span class="line">                DataColumn(label: Text(&#39;Source&#39;)),</span><br><span class="line">              ],</span><br><span class="line">            )</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-ddc03c9b05ad35b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Flutter-Chip/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/hexo/2020/08/26/Flutter-Chip/" class="post-title-link" itemprop="url">Flutter-Chip</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 16:55:26 / Modified: 16:55:44" itemprop="dateCreated datePublished" datetime="2020-08-26T16:55:26+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Flutter/" itemprop="url" rel="index"><span itemprop="name">Flutter</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="Chip"><a href="#Chip" class="headerlink" title="Chip"></a>Chip</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">class ChipDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _ChipDemoState createState() &#x3D;&gt; _ChipDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _ChipDemoState extends State&lt;ChipDemo&gt; &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;ChipDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: Container(</span><br><span class="line">        padding: EdgeInsets.all(16),</span><br><span class="line">        child: Column(</span><br><span class="line">          mainAxisAlignment: MainAxisAlignment.center,</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            Row(</span><br><span class="line">              mainAxisAlignment: MainAxisAlignment.center,</span><br><span class="line">              children: &lt;Widget&gt;[</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Life&#39;),</span><br><span class="line">                ),</span><br><span class="line">                SizedBox(width: 8,),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Sunset&#39;),</span><br><span class="line">                  backgroundColor: Colors.orange,</span><br><span class="line">                ),</span><br><span class="line">                SizedBox(width: 8,),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Wanghao&#39;),</span><br><span class="line">                  avatar: CircleAvatar(</span><br><span class="line">                    backgroundColor: Colors.grey,</span><br><span class="line">                    child: Text(&#39;王&#39;),</span><br><span class="line">                  ),</span><br><span class="line">                ),</span><br><span class="line">                SizedBox(width: 8,),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Wanghao&#39;),</span><br><span class="line">                  avatar: CircleAvatar(</span><br><span class="line">                    backgroundImage: AssetImage(</span><br><span class="line">                      &#39;resources&#x2F;images&#x2F;profilePhoto.jpg&#39;</span><br><span class="line">                    ),</span><br><span class="line">                  ),</span><br><span class="line">                ),</span><br><span class="line">              ],</span><br><span class="line">            )</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-2df0d64447be3811.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>在<code>Row</code>中的组件总长度如果超过屏幕长度，会出现警告，所以，我们可以将<code>Row</code>组件换成<code>Wrap</code>组件</p>
<h2 id="Wrap"><a href="#Wrap" class="headerlink" title="Wrap"></a>Wrap</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">class ChipDemo extends StatefulWidget &#123;</span><br><span class="line">  @override</span><br><span class="line">  _ChipDemoState createState() &#x3D;&gt; _ChipDemoState();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class _ChipDemoState extends State&lt;ChipDemo&gt; &#123;</span><br><span class="line">  @override</span><br><span class="line">  Widget build(BuildContext context) &#123;</span><br><span class="line">    return Scaffold(</span><br><span class="line">      appBar: AppBar(</span><br><span class="line">        title: Text(&#39;ChipDemo&#39;),</span><br><span class="line">      ),</span><br><span class="line">      body: Container(</span><br><span class="line">        padding: EdgeInsets.all(16),</span><br><span class="line">        child: Column(</span><br><span class="line">          mainAxisAlignment: MainAxisAlignment.center,</span><br><span class="line">          children: &lt;Widget&gt;[</span><br><span class="line">            Wrap(</span><br><span class="line">              spacing: 20, &#x2F;&#x2F; 主轴间距</span><br><span class="line">              runSpacing: 20, &#x2F;&#x2F; 次轴间距</span><br><span class="line">              children: &lt;Widget&gt;[</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Life&#39;),</span><br><span class="line">                ),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Sunset&#39;),</span><br><span class="line">                  backgroundColor: Colors.orange,</span><br><span class="line">                ),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Wanghao&#39;),</span><br><span class="line">                  avatar: CircleAvatar(</span><br><span class="line">                    backgroundColor: Colors.grey,</span><br><span class="line">                    child: Text(&#39;王&#39;),</span><br><span class="line">                  ),</span><br><span class="line">                ),</span><br><span class="line">                Chip(</span><br><span class="line">                  label: Text(&#39;Wanghao&#39;),</span><br><span class="line">                  avatar: CircleAvatar(</span><br><span class="line">                    backgroundImage: AssetImage(</span><br><span class="line">                      &#39;resources&#x2F;images&#x2F;profilePhoto.jpg&#39;</span><br><span class="line">                    ),</span><br><span class="line">                  ),</span><br><span class="line">                ),</span><br><span class="line">              ],</span><br><span class="line">            )</span><br><span class="line">          ],</span><br><span class="line">        ),</span><br><span class="line">      ),</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-52dad0a48a0eca01.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="Divider"><a href="#Divider" class="headerlink" title="Divider"></a>Divider</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Divider(</span><br><span class="line">  color: Colors.grey,</span><br><span class="line">  height: 32,</span><br><span class="line">  indent: 32, &#x2F;&#x2F; 缩进</span><br><span class="line">),</span><br></pre></td></tr></table></figure>
<h2 id="ActionChip"><a href="#ActionChip" class="headerlink" title="ActionChip"></a>ActionChip</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-a1260abb5398c787.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="FilterChip"><a href="#FilterChip" class="headerlink" title="FilterChip"></a>FilterChip</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Wrap(</span><br><span class="line">  spacing: 8,</span><br><span class="line">  children: _tags.map((tag) &#123;</span><br><span class="line">    return FilterChip(</span><br><span class="line">      label: Text(tag),</span><br><span class="line">      selected: _selected.contains(tag),</span><br><span class="line">      onSelected: (value) &#123;</span><br><span class="line">      setState(() &#123;</span><br><span class="line">        if (_selected.contains(tag)) &#123;</span><br><span class="line">            _selected.remove(tag);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">          _selected.add(tag);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">     &#125;,</span><br><span class="line">    );</span><br><span class="line">  &#125;).toList(),</span><br><span class="line">),</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-7882db84f269958c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h2 id="ChoiceChip"><a href="#ChoiceChip" class="headerlink" title="ChoiceChip"></a>ChoiceChip</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String _choice &#x3D; &#39;Lemon&#39;;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Wrap(</span><br><span class="line">  spacing: 8,</span><br><span class="line">  children: _tags.map((tag) &#123;</span><br><span class="line">    return ChoiceChip(</span><br><span class="line">      label: Text(tag),</span><br><span class="line">      selectedColor: Colors.black,</span><br><span class="line">      selected: _choice &#x3D;&#x3D; tag,</span><br><span class="line">      onSelected: (value) &#123;</span><br><span class="line">        setState(() &#123;</span><br><span class="line">          _choice &#x3D; tag;</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125;,</span><br><span class="line">    );</span><br><span class="line">  &#125;).toList(),</span><br><span class="line">),</span><br></pre></td></tr></table></figure>
<p><img src="https://upload-images.jianshu.io/upload_images/16731485-5acced8c71b0ec31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/hexo/page/4/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/hexo/">1</a><span class="space">&hellip;</span><a class="page-number" href="/hexo/page/4/">4</a><span class="page-number current">5</span><a class="page-number" href="/hexo/page/6/">6</a><span class="space">&hellip;</span><a class="page-number" href="/hexo/page/13/">13</a><a class="extend next" rel="next" href="/hexo/page/6/"><i class="fa fa-angle-right" aria-label="Next page"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
